
clear all

use "acs2005_2009", clear

bys serial famunit: egen householdmoved= max((migrate1d>=24)*(age>18))
keep if householdmoved
keep if age<=18

egen highest_hh_educd=rowmax(educd_mom educd_pop educd_mom2 educd_pop2 )





//bys serial famunit: egen highest_hh_educd=max(educd)

gen highest_hh_educ=0
replace highest_hh_educ=0     if highest_hh_educd==1
replace highest_hh_educ=0     if highest_hh_educd==2
replace highest_hh_educ=4     if highest_hh_educd==10
replace highest_hh_educ=6     if highest_hh_educd==21
replace highest_hh_educ=8     if highest_hh_educd==24
replace highest_hh_educ=9     if highest_hh_educd==30
replace highest_hh_educ=10    if highest_hh_educd==40
replace highest_hh_educ=11    if highest_hh_educd==50
replace highest_hh_educ=12    if highest_hh_educd==61
replace highest_hh_educ=12    if highest_hh_educd==62
replace highest_hh_educ=13    if highest_hh_educd==65
replace highest_hh_educ=14    if highest_hh_educd==71
replace highest_hh_educ=14    if highest_hh_educd==81
replace highest_hh_educ=16    if highest_hh_educd==101
replace highest_hh_educ=18    if highest_hh_educd==114
replace highest_hh_educ=19    if highest_hh_educd==115
replace highest_hh_educ=21    if highest_hh_educd==116


//bys serial famunit: egen hh_num_emp=total((empstat==1)*(age>18))

gen hh_num_emp=(empstat_pop==1)+(empstat_mom==1)+(empstat_pop2==1)+(empstat_mom2==1)

gen total_personal_income=inctot
replace total_personal_income=0 if inctot==9999999





egen hh_income=rsum(inctot_mom inctot_pop inctot_mom2 inctot_pop2)
//replace hh_income=500000 if hh_income>500000

//gen loghh_income=log(hh_income)

//keep if age<=18

collapse highest_hh_educ hh_num_emp hh_income  [w=afact2], by(age normal county state)

label var normal "Normal"
gen loghh_income=log(hh_income)

reghdfe highest_hh_educ i.age i.age#normal, absorb(state) cluster(state) nocon
//coefplot, keep(*normal) title("Highest Educ")
//graph rename educ, replace
preserve
parmest, fast
keep if substr(parm,-8,8)=="1.normal"
gen age=substr(parm,1,2)
replace age=substr(parm,1,1) if substr(parm,2,1)=="."
replace age=substr(parm,1,1) if substr(parm,2,1)=="b"
destring age, replace
twoway rspike min95 max95 age || scatter estimate age, legend(off) color(forest_green) yline(0,lcolor(gs12)) name(educ, replace)
graph export "selection_education.pdf", as(pdf) replace
restore


reghdfe hh_num_emp i.age i.age#normal, absorb(state) cluster(state) nocon
// coefplot, keep(*normal) title("Num Employed")
// graph rename employed, replace
preserve
parmest, fast
keep if substr(parm,-8,8)=="1.normal"
gen age=substr(parm,1,2)
replace age=substr(parm,1,1) if substr(parm,2,1)=="."
replace age=substr(parm,1,1) if substr(parm,2,1)=="b"
destring age, replace
twoway rspike min95 max95 age || scatter estimate age, legend(off) color(forest_green) yline(0,lcolor(gs12)) name(employed, replace)
graph export "selection_employment.pdf", as(pdf) replace
restore

reghdfe loghh_income i.age i.age#normal, absorb(state) cluster(state) nocon
// coefplot, keep(*normal) title("Log Income")
// graph rename income, replace

preserve
parmest, fast
keep if substr(parm,-8,8)=="1.normal"
gen age=substr(parm,1,2)
replace age=substr(parm,1,1) if substr(parm,2,1)=="."
replace age=substr(parm,1,1) if substr(parm,2,1)=="b"
destring age, replace
twoway rspike min95 max95 age || scatter estimate age, legend(off) color(forest_green) yline(0,lcolor(gs12)) name(income, replace)
graph export "selection_income.pdf", as(pdf) replace
restore

gen agenormal=age*normal

reghdfe highest_hh_educ normal agenormal, absorb(state#age) cluster(state) nocon 
est sto educ
reghdfe hh_num_emp normal agenormal, absorb(state#age) cluster(state) nocon
est sto emp
reghdfe loghh_income normal agenormal, absorb(state#age) cluster(state) nocon
est sto income

label var highest_hh_educ "Highest Education"
label var hh_num_emp "Num. Adults Employed"
label var loghh_income "Log Parental Income"
label var age "Age of Child"
label var normal "Normal"
label var agenormal "Age X Normal"

esttab educ emp income, se label star(+ .1 * .05 ** .01 *** .001)
esttab educ emp income using "selection_acs.tex", se label tex replace note("Standard errors clustered by state") star(+ .1 * .05 ** .01 *** .001)

graph combine educ employed income


